package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jayc on 2017/4/10.
 * Email:cnjiaqi@sina.com
 */
public class SchoolAdapter extends BaseAdapter {
    private Context mContext;
    private List mList = new ArrayList();

    public SchoolAdapter(Context context) {
        mContext = context;
    }


    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    public void updateData(ArrayList list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.schoollist_item, parent, false);
            childHolder.schoolNameText = (TextView) convertView.findViewById(R.id.school_item_name);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        String info = (String) mList.get(position);
        childHolder.schoolNameText.setText(info);
        childHolder.schoolNameText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick((String) mList.get(position));
                }
            }
        });
        return convertView;
    }

    class ChildHolder {
        ImageView headImg;
        TextView schoolNameText;
    }

    public interface SchoolListAdapterListener {
        public void onClick(String school);
    }

    private SchoolListAdapterListener mListener;

    public void setAdapterListener(SchoolListAdapterListener listener) {
        mListener = listener;
    }
}
